package com.jm3.riscar3.helpers;

public class StringToBinaryTranslator {

	public static int HexToByte(String hex) throws Exception
	{
		int length = hex.length();
		int result = 0;
		
		for(int i = 1; i <= length; i++)
		{
			char ci = hex.charAt(i-1);
			switch (ci) {
			case '0':
			case '1':
			case '2':
			case '3':
			case '4':
			case '5':
			case '6':
			case '7':
			case '8':
			case '9':	
				result += Integer.parseInt("" + ci) * Math.pow(16, length - i);
				break;
			case 'A':
				result += 10 * Math.pow(16, length - i);
				break;
			case 'B':
				result += 11 * Math.pow(16, length - i);
				break;
			case 'C':
				result += 12 * Math.pow(16, length - i);
				break;
			case 'D':
				result += 13 * Math.pow(16, length - i);
				break;
			case 'E':
				result += 14 * Math.pow(16, length - i);
				break;
			case 'F':
				result += 15 * Math.pow(16, length - i);
				break;	
			default:
				throw new Exception();
			}
		}
		
		return result;
	}
	
}
